package com.huawei.secure.android.common.encrypt.aes;

import android.text.TextUtils;
import android.util.Log;
import com.huawei.secure.android.common.encrypt.utils.EncryptUtil;
import com.huawei.secure.android.common.encrypt.utils.a;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class AesCbc {
    private static final String TAG = "AesCbc";
    private static final String f = "security:";
    private static final String g = "AES/CBC/PKCS5Padding";
    private static final String h = "AES";
    private static final String i = "";
    private static final int j = 16;
    private static final int k = 16;

    private AesCbc() {
    }

    public static String encrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Log.e(TAG, "cbc encrypt param is not right");
            return "";
        }
        byte[] l = a.l(str2);
        if (l.length >= 16) {
            return encrypt(str, l);
        }
        Log.e(TAG, "key length is not right");
        return "";
    }

    public static String encrypt(String str, byte[] bArr) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16) {
            Log.e(TAG, "cbc encrypt param is not right");
            return "";
        }
        byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(16);
        byte[] a = a(str, bArr, generateSecureRandom);
        if (a == null || a.length == 0) {
            return "";
        }
        return a(a.f(generateSecureRandom), a.f(a));
    }

    public static String encrypt(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            Log.e(TAG, "cbc encrypt param is not right");
            return "";
        }
        byte[] l = a.l(str2);
        byte[] l2 = a.l(str3);
        if (l.length >= 16 && l2.length >= 16) {
            return encrypt(str, l, l2);
        }
        Log.e(TAG, "key length or ivParameter length is not right");
        return "";
    }

    public static String encrypt(String str, byte[] bArr, byte[] bArr2) {
        if (!TextUtils.isEmpty(str) && bArr != null && bArr.length >= 16 && bArr2 != null && bArr2.length >= 16) {
            return a.f(a(str, bArr, bArr2));
        }
        Log.e(TAG, "cbc encrypt param is not right");
        return "";
    }

    private static byte[] a(String str, byte[] bArr, byte[] bArr2) {
        if (TextUtils.isEmpty(str)) {
            Log.e(TAG, "cbc encrypt param is not right");
            return new byte[0];
        }
        try {
            return encrypt(str.getBytes("UTF-8"), bArr, bArr2);
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, " cbc encrypt data error" + e.getMessage());
            return new byte[0];
        }
    }

    public static String decrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Log.e(TAG, "content or key is null");
            return "";
        }
        byte[] l = a.l(str2);
        if (l.length >= 16) {
            return decrypt(str, l);
        }
        Log.e(TAG, "key length is not right");
        return "";
    }

    public static String decrypt(String str, byte[] bArr) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16) {
            return "";
        }
        Object a = a(str);
        String b = b(str);
        if (!TextUtils.isEmpty(a) && !TextUtils.isEmpty(b)) {
            return decrypt(b, bArr, a.l(a));
        }
        Log.e(TAG, "ivParameter or encrypedWord is null");
        return "";
    }

    public static String decrypt(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            Log.e(TAG, "cbc decrypt param is not right");
            return "";
        }
        byte[] l = a.l(str2);
        byte[] l2 = a.l(str3);
        if (l.length >= 16 && l2.length >= 16) {
            return decrypt(str, l, l2);
        }
        Log.e(TAG, "key length or ivParameter is not right");
        return "";
    }

    public static String decrypt(String str, byte[] bArr, byte[] bArr2) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || bArr2 == null || bArr2.length < 16) {
            Log.e(TAG, "cbc decrypt param is not right");
            return "";
        }
        try {
            return new String(decrypt(a.l(str), bArr, bArr2), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, " cbc decrypt data error" + e.getMessage());
            return "";
        }
    }

    private static String a(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return "";
        }
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(str2.substring(0, 6));
            stringBuilder.append(str.substring(0, 6));
            stringBuilder.append(str2.substring(6, 10));
            stringBuilder.append(str.substring(6, 16));
            stringBuilder.append(str2.substring(10, 16));
            stringBuilder.append(str.substring(16));
            stringBuilder.append(str2.substring(16));
            return stringBuilder.toString();
        } catch (Exception e) {
            Log.e(TAG, "mix exception: " + e.getMessage());
            return "";
        }
    }

    public static byte[] encrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || bArr3 == null || bArr3.length < 16) {
            Log.e(TAG, "cbc encrypt param is not right");
            return new byte[0];
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr2, h);
            Cipher instance = Cipher.getInstance(g);
            instance.init(1, secretKeySpec, new IvParameterSpec(bArr3));
            return instance.doFinal(bArr);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, " cbc encrypt data error" + e.getMessage());
            return new byte[0];
        }
    }

    public static byte[] decrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || bArr3 == null || bArr3.length < 16) {
            Log.e(TAG, "cbc decrypt param is not right");
            return new byte[0];
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr2, h);
            Cipher instance = Cipher.getInstance(g);
            instance.init(2, secretKeySpec, new IvParameterSpec(bArr3));
            return instance.doFinal(bArr);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, " cbc decrypt data error" + e.getMessage());
            return new byte[0];
        }
    }

    public static byte[] encrypt(byte[] bArr, byte[] bArr2) {
        byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(16);
        return a(generateSecureRandom, encrypt(bArr, bArr2, generateSecureRandom));
    }

    public static byte[] decrypt(byte[] bArr, byte[] bArr2) {
        return decrypt(b(bArr), bArr2, a(bArr));
    }

    private static byte[] a(byte[] bArr, byte[] bArr2) {
        Object obj = new byte[(bArr.length + bArr2.length)];
        System.arraycopy(bArr, 0, obj, 0, bArr.length);
        System.arraycopy(bArr2, 0, obj, bArr.length, bArr2.length);
        return obj;
    }

    private static byte[] a(byte[] bArr) {
        Object obj = new byte[16];
        System.arraycopy(bArr, 0, obj, 0, 16);
        return obj;
    }

    private static byte[] b(byte[] bArr) {
        Object obj = new byte[(bArr.length - 16)];
        System.arraycopy(bArr, 16, obj, 0, bArr.length - 16);
        return obj;
    }

    private static String a(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(str.substring(6, 12));
            stringBuilder.append(str.substring(16, 26));
            stringBuilder.append(str.substring(32, 48));
            return stringBuilder.toString();
        } catch (Exception e) {
            Log.e(TAG, "getIv exception : " + e.getMessage());
            return "";
        }
    }

    private static String b(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(str.substring(0, 6));
            stringBuilder.append(str.substring(12, 16));
            stringBuilder.append(str.substring(26, 32));
            stringBuilder.append(str.substring(48));
            return stringBuilder.toString();
        } catch (Exception e) {
            Log.e(TAG, "get encryptword exception : " + e.getMessage());
            return "";
        }
    }

    static String c(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        return str.indexOf(f) == -1 ? "" : str.substring(f.length());
    }

    static byte[] c(byte[] bArr) {
        String str;
        String str2 = "";
        try {
            str = new String(bArr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "stripCryptHead: exception : " + e.getMessage());
            str = str2;
        }
        if (!str.startsWith(f)) {
            return new byte[0];
        }
        if (bArr.length <= f.length()) {
            return new byte[0];
        }
        byte[] bArr2 = new byte[(bArr.length - f.length())];
        System.arraycopy(bArr, f.length(), bArr2, 0, bArr2.length);
        return bArr2;
    }

    public static String decryptWithCryptHead(String str, byte[] bArr) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16) {
            return "";
        }
        String c = c(str);
        if ("".equals(c)) {
            return "";
        }
        int indexOf = c.indexOf(58);
        if (indexOf >= 0) {
            return decrypt(a.f(a.l(c.substring(indexOf + 1))), bArr, a.l(c.substring(0, indexOf)));
        }
        Log.e(TAG, " cbc cipherText data missing colon");
        return "";
    }

    public static String decryptWithCryptHead(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr2 == null || bArr2.length < 16) {
            return "";
        }
        Object c = c(bArr);
        if (c.length == 0) {
            return "";
        }
        int d = d(c);
        if (d >= 0) {
            byte[] copyOf = Arrays.copyOf(c, d);
            int length = (c.length - copyOf.length) - ":".length();
            Object obj = new byte[length];
            System.arraycopy(c, d + 1, obj, 0, length);
            return decrypt(a.f(obj), bArr2, copyOf);
        }
        Log.e(TAG, " cbc cipherText data missing colon");
        return "";
    }

    private static int d(byte[] bArr) {
        if (bArr[16] == (byte) 58) {
            return 16;
        }
        return -1;
    }
}
